package tq.com.tqteacher.activity.newPublish;

import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.browser.DetailPicsActivity;

import java.util.ArrayList;

import tq.com.tqcom.base.BaseFragment;
import tq.com.tqcom.base.BaseRecyclerFragment;
import tq.com.tqcom.base.BaseTitleActivity;
import tq.com.tqcom.net.TQPictureUpload;
import tq.com.tqcom.util.TQEventBus;
import tq.com.tqcom.view.TQHeader5View;
import tq.com.tqteacher.adapt.newPublish.TQLetShareContentAdapter;
import tq.com.tqteacher.discuss.activity.TQDiscussMsgActivity;
import tq.com.tqteacher.model.TQAtachModel;
import tq.com.tqteacher.model.TQLetShareModel;
import tq.com.tqteacher.model.TQPublishType;

public class TQLetShareContent extends BaseTitleActivity {
    private TQLetShareContentFragment mLetShareContentFragment = new TQLetShareContentFragment();


    @Override
    protected void initHeader(TQHeader5View pHeader5View) {
        super.initHeader(pHeader5View);
        pHeader5View.getTitleView().setText("分享");
    }

    @Override
    protected BaseFragment fragment() {
        return mLetShareContentFragment;
    }

    static public class TQLetShareContentFragment extends BaseRecyclerFragment {
        private TQLetShareContentAdapter mLetShareContentAdapter;
        private TQEventBus mEventBus;

        private int mCurrentAttachId;
        @Override
        protected void initRecycleView(RecyclerView pRecyclerView) {
            super.initRecycleView(pRecyclerView);
            pRecyclerView.setBackgroundColor(Color.parseColor("#f6f7f8"));
            pRecyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));

            if(mLetShareContentAdapter == null){
                mLetShareContentAdapter = new TQLetShareContentAdapter(getContext());
                mLetShareContentAdapter.setClickListener(mClickListener);
            }
            pRecyclerView.setAdapter(mLetShareContentAdapter);

            if(mEventBus == null){
                mEventBus = new TQEventBus(mEventLisnter);
                mEventBus.register();
            }
        }

        @Override
        public void onStart() {
            super.onStart();
            if(TQPublishType.getSharePublishItem() instanceof TQLetShareModel){
                TQLetShareModel lModel = ((TQLetShareModel)TQPublishType.getSharePublishItem());
                ArrayList<TQAtachModel> lTQAtachModels = ((TQLetShareModel)TQPublishType.getSharePublishItem()).getAttaches();
                mLetShareContentAdapter.updateAttanchInfo(lModel.getMaxUser(), lModel.getCurrentAttachement());
                mLetShareContentAdapter.replaceAttach(lTQAtachModels);
            }
        }

        @Override
        public void onDestroy() {
            super.onDestroy();
            mEventBus.unregister();
        }

        private TQLetShareContentAdapter.IClickListener mClickListener = new TQLetShareContentAdapter.IClickListener() {
            @Override
            public void capture() {
                TQLetShareCaptureActivity.show(TQLetShareContentFragment.this.getActivity());
            }

            @Override
            public void openPicture(String id) {
                ArrayList<String> lBrowserPicItems = new ArrayList<>();
                lBrowserPicItems.add(TQPictureUpload.downloadPath(id));

                DetailPicsActivity.jumpToThis(TQLetShareContentFragment.this.getActivity(), lBrowserPicItems, 0);
            }

            @Override
            public void openAttachComm(int id) {
                int mId = TQPublishType.getSharePublishItem().getSeq_id();
                mCurrentAttachId = id;

                TQDiscussMsgActivity.show(mId, id, null, TQLetShareContentFragment.this.getActivity());
            }
        };

        private TQEventBus.IEventLisnter mEventLisnter = new TQEventBus.IEventLisnter() {
            @Override
            public void onEvnet(TQEventBus.TQEventMessage msg) {
                if(mCurrentAttachId == 0) return;

                TQAtachModel lAtachModel = mLetShareContentAdapter.getAttachById(mCurrentAttachId);
                if(lAtachModel != null){
                    int count = lAtachModel.getCommentCount();
                    if(msg.getOperator() == TQEventBus.TQEventMessage.Opertor.add){
                        count++;
                    }else {
                        count--;
                    }

                    lAtachModel.setCommentCount(count);
                    mLetShareContentAdapter.updateAttachInfo(lAtachModel);
                }
            }
        };
    }
}
